﻿$(function () {

    //Lync integration is driven by the ActiveX Control "Name.NameCtrl.1" located in C:\Program Files\Microsoft Office\Office14\NAME.dll
    //ActiveX only supported by IE.
    //LYNC must be installed on the client machine.
    if ($.browser.msie && nameCtrl) {

        //Render status images
        $('.lyncContainer').each(function () {

            //Get the id of the span
            var id = $(this).attr('id');

            //Get the identifier (email value)
            var identifier = $('input[type = hidden]', $(this)).val();

            if (nameCtrl.PresenceEnabled) {
                //Get the status of the person using the identifier.
                var status = nameCtrl.GetStatus(identifier, id);
                //Show the status image
                ShowStatusImage($(this), status, id);
            }
        });


        //Hide the lync context menu when the user scrolls.
        $(window).scroll(function () {
            HideOOUI();
        });


        //Show the LYNC context menu when the user hovers the status image.
        //Hide the Lync context menu when the mouse leaves the status image.
        $('.lync').hover(function () {

            //Try to get the span that is rendered after the status image.
            var span = $(this).next('span');
            if (!span[0]) {
                //If getting the span failed it is possible the span is rendered in front of the image.
                span = $(this).prev('span');
            }

            //Get the x & y coordinates to position the LYNC context menu over the status image.
            var coordinates = GetContextPosition($(this), span);

            //Get the identifier (email value)
            var identifier = span.children('input[type = hidden]').val();

            //Show the LYNC context menu
            ShowOOUI(identifier, coordinates.left, coordinates.top);

        }, function () {
            //Hide the lync context menu on mouse out
            HideOOUI();
        });
    }

});

//No need to load the ActiveX Control if the user agent is not IE
if ($.browser.msie) {

    try {
        //Load the ActiveX Control
        var nameCtrl = new ActiveXObject('Name.NameCtrl.1');

        //Bind the onStatusChange event handler to the OnStatusChange event
        //If a person's LYNC status changes this event will be triggered.
        if (nameCtrl && nameCtrl.PresenceEnabled) {
            nameCtrl.OnStatusChange = onStatusChange;
        }
    }
    catch (e) {
        debug("could not load ActiveXObject('Name.NameCtrl.1')");
    }
}

//Handles the OnStatusChange event.
function onStatusChange(name, status, id) {
    //Get the DOM element using the id.
    var element = $('#' + id);
    //Update the status image.
    ShowStatusImage(element, status, id);
}

//Show the LYNC context menu
function ShowOOUI(email, x, y) {
    nameCtrl.ShowOOUI(email, 0, x, y);
}

//Hide the LYNC context menu
function HideOOUI() {
    nameCtrl.HideOOUI();
}

//Renders the status image using the LYNC status code
function ShowStatusImage($element, status, id) {

    //Try to get the status img element that is rendered after the span.
    var imgElement = $element.next('img');
    if (!imgElement[0])
    //If getting the status img element failed it is possible the status img element is rendered in front of the span.
        imgElement = $element.prev('img');

    //Construct the path to the correct image representing the LYNC status.
    var statusImage = '../img/' + GetStatusImage(status);

    if (imgElement[0]) {
        //If the status img element is already rendered we change it's src attribute value to the 
        //path of the new image representing the current status.
        imgElement.attr('src', statusImage);
    }
    else {
        //If the status img element is not yet rendered we construct the html for the img element.
        imgElement = '<img class="lync" src="' + statusImage + '" style="padding-right: 5px; margin-bottom: -1px" />';

        //Get the position part from the id
        var positioning = id.split('_')[1];

        //Add the img element to the dom using the correct positioning.
        if (positioning === 'right') {
            $element.parent().append(imgElement);
        }
        else {
            $element.parent().prepend(imgElement);
        }

    }
}

//Returns the name of the image representing a LYNC status
function GetStatusImage(status) {
    var img = "imnhdr.gif";
    switch (status) {
        //Online 
        case 0:
            img = "imnon.png";
            break;
        //Online (Out of Office) 
        case 11:
            img = "imnonoof.png";
            break;
        //Offline 
        case 1:
            img = "imnhdr.gif";
            break;
        //Offline (Out of Office)  
        case 12:
            img = "imnoffoof.png";
            break;
        //Away 
        case 2:
            img = "imnaway.png";
            break;
        //Away (Out of Office)  
        case 13:
            img = "imnawayoof.png";
            break;
        //Busy 
        case 3:
            img = "imnbusy.png";
            break;
        //Busy (Out of Office)  
        case 14:
            img = "imnbusyoof.png";
            break;
        //Be right back 
        case 4:
            img = "imnaway.png";
            break;
        //On the phone 
        case 5:
            img = "imnbusy.png";
            break;
        //Out to lunch 
        case 6:
            img = "imnaway.png";
            break;
        //??? 
        case 7:
            img = "imnbusy.png";
            break;
        //??? 
        case 8:
            img = "imnaway.png";
            break;
        //Do not distrub 
        case 9:
            img = "imndnd.png";
            break;
        //Do not distrub (Out of Office)  
        case 15:
            img = "imndndoof.png";
            break;
        //???  
        case 10:
            img = "imnbusy.png";
            break;
        //Idle 
        case 16:
            img = "imnidle.png";
            break;
        //Idle (Out of office) 
        case 17:
            img = "imnidleoof.png";
            break;
        //Blocked 
        case 18:
            img = "imnblocked.png";
            break;
        //Idle-Busy 
        case 19:
            img = "imnidlebusy.png";
            break;
        //Idle/Busy (Out of Office)  
        case 20:
            img = "imnidlebusyoof.png";
            break;
    }
    return img;
}

//Get the position for displaying the LYNC context menu
function GetContextPosition($element, span) {

    //Get the current vertical position of the scroll bar
    var scroll = $(window).scrollTop();
    //Get the position of the container element
    var offset = $element.parent().offset();
    //Get the position part from the id
    var positioning = span.attr('id').split('_')[1];

    //If we need to position the LYNC context menu right of the 
    //span we add the width of the span to the left offset
    if (positioning === 'right') {
        offset.left = offset.left + span.width();
    }

    //Subtract the scroll amount from the top offset
    offset.top = offset.top - scroll;

    //return the calculated position
    return offset;
}